

from PIL import Image, ImageDraw
import os
import random

# 创建输出目录
output_dir = "C:\\Users\\Administrator\\Desktop\\bmps"
os.makedirs(output_dir, exist_ok=True)


# 生成随机颜色
def random_color():
    return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))


# 生成工程相关的随机图案
def create_engineering_image(size):
    img = Image.new('RGB', size, color=random_color())
    draw = ImageDraw.Draw(img)

    # 随机绘制网格
    if random.choice([True, False]):
        grid_size = random.randint(3, 6)
        for x in range(0, size[0], grid_size):
            draw.line([(x, 0), (x, size[1])], fill=random_color())
        for y in range(0, size[1], grid_size):
            draw.line([(0, y), (size[0], y)], fill=random_color())

    # 随机绘制矩形或方块
    if random.choice([True, False]):
        x1 = random.randint(0, size[0] // 2)
        y1 = random.randint(0, size[1] // 2)
        x2 = random.randint(x1, size[0])
        y2 = random.randint(y1, size[1])
        draw.rectangle([x1, y1, x2, y2], fill=random_color(), outline=random_color())

    # 随机绘制线条
    if random.choice([True, False]):
        start_point = (random.randint(0, size[0]), random.randint(0, size[1]))
        end_point = (random.randint(0, size[0]), random.randint(0, size[1]))
        draw.line([start_point, end_point], fill=random_color(), width=random.randint(1, 3))

    return img


# 生成10个22x22的BMP图片
for i in range(10):
    img = create_engineering_image((22, 22))
    file_path = os.path.join(output_dir, f"engineering_image_{i + 1}.bmp")
    img.save(file_path, format="BMP")

print(f"10个22x22的工程相关BMP图片已生成在目录: {output_dir}")